using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.SharePoint;

namespace ARF.Web.Controls
{
    public class ColumnsAsXml : XmlProvider
    {
        private string _internalColumnNames;

        public string InternalColumnNames
        {
            get { return _internalColumnNames; }
            set { _internalColumnNames = value; }
        }

        protected override void BuildXml()
        {
            try
            {
                if (string.IsNullOrEmpty(InternalColumnNames))
                {
                    XmlAsString = "<Error>No columns secified</Error>";
                    return;
                }

                string[] arColumns = InternalColumnNames.Split(';');

                XmlDocument oDoc = new XmlDocument();
                oDoc.LoadXml("<Columns>");

                SPListItem oCurrentItem = SPContext.Current.ListItem;

                foreach(string sColumn in arColumns)
                {
                    XmlElement oColumn = oDoc.CreateElement("Column");
                    oColumn.SetAttribute("Name", sColumn);
                    oColumn.InnerText = oCurrentItem[sColumn].ToString();
                    oDoc.DocumentElement.AppendChild(oColumn);
                }

                XmlAsObject = oDoc;
            }
            catch (Exception ex)
            {
                Log.Trace(ex);
                XmlAsString = string.Format("<Error>{0}</Error>", ex.Message);
                return;
            }
        }
    }
}
